package demo3;

import java.util.Arrays;
import java.util.Stack;
public class MyStack {
    public int[] elem;
    public int usedSize;

    public MyStack() {
        this.elem = new int[5];
    }

    public void push (int val) {
        if (isFull()) {
            this.elem = Arrays.copyOf(this.elem, usedSize * 2);
        }
        this.elem[usedSize++] = val;
    }

    public int pop () {
        if (empty()) {
            throw new RuntimeException("删除失败 栈为空");
        }
        usedSize--;
        return this.elem[usedSize];
    }

    public int peek() {
        if (empty()) {
            throw new RuntimeException("删除失败 栈为空");
        }
        return this.elem[usedSize - 1];
    }


    public boolean empty() {
        if (usedSize == 0) {
            return true;
        }
        return false;
    }

    public boolean isFull() {
        return this.elem.length == usedSize;
    }



    public static void main(String[] args) {
        //写一下MyStack
        MyStack myStack = new MyStack();
        myStack.push(12);
        myStack.push(12);
        myStack.push(12);
        System.out.println(myStack.pop());
        System.out.println(myStack.pop());
        System.out.println(myStack.pop());
        System.out.println(myStack.peek());

    }
}
